var varScript_StaticResourceURL = "/static-res/ko";
var varScript_StaticResource_Globalloc_URL = "/static-res/ko";

(function($) {
	
	$.doitBox = {version: '1.0.0'};
	
	$.fn.doitBox = function(settings) {
		settings = jQuery.extend( {
			overlayBgColor : '#000', 
			overlayOpacity : 0.6,   
			imageLoading 	: varScript_StaticResourceURL + '/images/box/lightbox-ico-loading.gif',  
			imageBtnClose 	: varScript_StaticResourceURL + '/images/box/lightbox-btn-close.gif',
			imageBlank 		: varScript_StaticResourceURL + '/images/box/lightbox-blank.gif',
			containerBorderSize : 0, 
			containerResizeSpeed : 400,
			options : null
		}, settings);
		
		$.doitBox.start = function(options) {			
			settings.options = options;
			if(settings.options.frmmethod == undefined) {
				settings.options.frmmethod = "POST";
			}
			
			if(settings.options.scroll == undefined) {
				settings.options.scroll = "yes";
			}
			_start();
		}
		
		$.doitBox.finish = function(settings) {
			_finish();
		}
		
		$.doitBox.resize = function(settings) {
			_resize();
		}
		
		$.doitBox.autoSubResize = function(settings) {
			_resize();
		}
		
		function _start() {
			$('embed, object, select').css( {
				'visibility' : 'hidden'
			});
			_set_interface();			
			_set_image_to_view();
			
			$(window).resize(function() {
				var arrPageSizes = ___getPageSize(true);
				
				$('#jquery-overlay').css( {
					backgroundColor : settings.overlayBgColor,
					opacity : settings.overlayOpacity,
					width : arrPageSizes[0],
					height : arrPageSizes[1]
				}).fadeIn();
			});
			
		}

		function _set_interface() {
			
			var _title;
			var _closeBtnClass;
			if(settings.options.title == undefined){
				_title = '';
				_closeBtnClass = 'btn_close2';
			}else{
				_title = '<div class="title" id="lightbox-title"></div>';
				_closeBtnClass = 'btn_close';
			}
			
			var _html = '';
			
			_html +=
					'<div id="jquery-lightbox">' +
					'<div id="lightbox-container-image-box">' +
					
					'<div class="layer_pop w'+settings.options.width+'">' +
				    '<div class="layer_box">' +
				    _title +
				    '<div id="lightbox-image"></div>' +
					'<a href="#ClosePopup" class="'+_closeBtnClass+'" id="lightbox-secNav-btnTopClose" >창 닫기</a>'+
					
					'</div>' +
					'</div>' +
					
					'</div>' +
				    '</div>' +
				    
					'<form id="box_form" name="box_form" method="'+settings.options.frmmethod+'" target="ibox_img"></form>'	
					;
			
			
			$('body').append('<div id="jquery-overlay"></div>');
			
			if($('body').hasClass('content')){
				$('.content').append(_html);
			}else if($('body').hasClass('m_content')){
				$('.m_content').append(_html);
			}else{
				$('body').append(_html);
			}

			var arrPageSizes = ___getPageSize(false);
			$('#jquery-overlay').css( {
				backgroundColor : settings.overlayBgColor,
				opacity : settings.overlayOpacity,
				width : arrPageSizes[0],
				height : arrPageSizes[1]
			}).fadeIn();
			var arrPageScroll = ___getPageScroll();
			$('#jquery-lightbox').css( {
				top : arrPageScroll[1] + (arrPageSizes[3] / 10) + 20,
				left : arrPageScroll[0]
			}).show();
			
			$('#lightbox-secNav-btnClose').unbind().click( function() {
				if (settings.options.cancle)
					settings.options.cancle();
				_finish();
				return false;
			});
			
			$('#lightbox-secNav-btnTopClose').unbind().click( function() {
				if (settings.options.cancle)
					settings.options.cancle();
				_finish();
				return false;
			});
		}
		
		function _set_image_to_view() { 
			$('#lightbox-loading').show();
			$('#lightbox-image,#lightbox-container-image-data-box').hide();
			var boxWidth = settings.options.width == null ? 100 : settings.options.width;
			var boxHeight = settings.options.height == null ? 100 : settings.options.height;
			_resize_container_image_box(boxWidth, boxHeight, false);
		}
		function _resize_container_image_box(boxWidth, boxHeight, flag) {
			
			var intCurrentWidth = $('#lightbox-container-image-box').width();
			var intCurrentHeight = $('#lightbox-container-image-box').height();
			var intWidth = (boxWidth + (settings.containerBorderSize * 2));
			var intHeight = (boxHeight + (settings.containerBorderSize * 2));
			
			var intDiffW = intCurrentWidth - intWidth;
			var intDiffH = intCurrentHeight - intHeight;
			
			if(settings.options.title == undefined){
				intHeight = intHeight + 20;
			}else{
				intHeight = intHeight + 63;
			}
			
			//alert("boxHeight:"+boxHeight+"\n intHeight:"+intHeight+"\n intDiffH:"+intDiffH+"\n intCurrentHeight:"+intCurrentHeight);
			
			//$("body").css({"overflow-x" : "hidden"}); /*IE7에서 스크롤두개 생기는버그때문에 주석*/
			$('#lightbox-container-image-box').animate( {
				width : intWidth,
				height : intHeight
			}, settings.containerResizeSpeed, function() {
				if ($("#ibox_iframe").length == 0) {
					_show_image(boxHeight);
					if (settings.options.title){
						$("#lightbox-title").append(settings.options.title);
					}
				} else {
					$("#ibox_iframe").height(boxHeight);
				}
				if (flag) {
					var arrPageSizes = ___getPageSize(true);
					$('#jquery-overlay').css( {
						backgroundColor : settings.overlayBgColor,
						opacity : settings.overlayOpacity,
						width : arrPageSizes[0],
						height : arrPageSizes[1]
					}).fadeIn();
					//$("body").css({"overflow-x" : "auto"});/*IE7에서 스크롤두개 생기는버그때문에 주석*/
				}
			});
			if ((intDiffW == 0) && (intDiffH == 0)) {
				if ($.browser.msie) {
					___pause(250);
				} else {
					___pause(100);
				}
			}
			$('#lightbox-container-image-data-box').css( {
				width : boxWidth
			});
		}
		function _show_image(boxHeight) {
			$('#lightbox-loading').hide();
			$('#lightbox-image').fadeIn(function() {
				$('#lightbox-image').append('<iframe id="ibox_iframe" name="ibox_iframe" width="100%" height="' + boxHeight + 'px" scrolling="'+settings.options.scroll+'" frameborder="0" />');				
				for (var i = 0 ; i < settings.options.params.length ; i++) {
					var paramData = settings.options.params[i].split("=");
					var inputHTML  =  '<input type="hidden"';
                    	inputHTML +=    'id="'+paramData[0]+'" ';
                    	inputHTML +=    'name="'+paramData[0]+'" ';
                    	inputHTML +=    'value="'+paramData[1]+'" />';
                    	
                    $("#box_form").append(inputHTML);
				}
				
				if (settings.options.method) {
					var inputHTML  =  '<input type="hidden"';
                	inputHTML +=    'id="method" ';
                	inputHTML +=    'name="method" ';
                	inputHTML +=    'value="'+settings.options.method+'" />';
                	$("#box_form").append(inputHTML);
				}
				
				$("#box_form").attr({
										action : settings.options.url, 
										target : "ibox_iframe"
									}).submit();
				
				$("#box_form").remove();
				
				if (settings.options.autoresize) {
					$('#ibox_iframe').unbind().load( function() {
						_resize();
					});
				}
				
				_show_image_data();
			});
		};
		
		function _resize() {
			//var $element = $get('ibox_iframe');
			var $element = document.getElementById("ibox_iframe");
			/*
			alert("offsetHeight:"+$element.contentWindow.document.body.offsetHeight);
			alert("scrollHeight:"+$element.contentWindow.document.body.scrollHeight);
			alert("height:"+$element.contentWindow.document.body.height);
			*/
			if($element.contentWindow.document.body.offsetHeight>$element.contentWindow.document.body.scrollHeight){
				height = $element.contentWindow.document.body.offsetHeight+63;
			}else{
				height = $element.contentWindow.document.body.scrollHeight;
			}
			
			$("#ibox_iframe").height(height);
			//var boxWidth = settings.options.width == null ? 100 : $element.contentWindow.document.body.offsetWidth;
			var boxWidth = settings.options.width == null ? 100 : settings.options.width;
			_resize_container_image_box(boxWidth, height, true);
		}
		
		function _show_image_data() {
			$("#lightbox-container-image-box").addClass("noBottomBorder");
			$('#lightbox-container-image-data-box').slideDown("slow");
		}
		function _finish() {
			$('#jquery-lightbox').remove();
			$('#jquery-overlay').fadeOut(function() {
				$('#jquery-overlay').remove();
			});
			$('embed, object, select').css( {
				'visibility' : 'visible'
			});
		}
		function ___getPageSize(flag) {
			
			var xScroll, yScroll;			
			xScroll = $(document).width();
			yScroll = $(document).height();
			
			var windowWidth, windowHeight;			
			windowWidth = $(window).width();
			windowHeight = $(window).height();
			
			//alert("yScroll:"+yScroll+" windowHeight:"+windowHeight+" xScroll:"+xScroll+" windowWidth:"+windowWidth+"");
			
			if (yScroll < (windowHeight+20) ) {
				pageHeight = windowHeight;
			} else {
				pageHeight = yScroll;
			}
			if (xScroll < (windowWidth +20) ) {
				pageWidth = windowWidth;				
			} else {
				pageWidth = xScroll;
			}
			
			arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
			return arrayPageSize;
		}
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement
					&& document.documentElement.scrollTop) {  
																 
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) { 
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;
			}
			arrayPageScroll = new Array(xScroll, yScroll);
			return arrayPageScroll;
		}
		function ___pause(ms) {
			var date = new Date();
			curDate = null;
			do {
				var curDate = new Date();
			} while (curDate - date < ms);
		}
		;
	};
})(jQuery); 

$().doitBox();

function t___getPageSize(flag) {
	
	var xScroll, yScroll;			
	xScroll = $(document).width();
	yScroll = $(document).height();
	
	var windowWidth, windowHeight;			
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	
	//alert("yScroll:"+yScroll+" windowHeight:"+windowHeight+" xScroll:"+xScroll+" windowWidth:"+windowWidth+"");
	
	if (yScroll < (windowHeight+20) ) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if (xScroll < (windowWidth +20) ) {
		pageWidth = windowWidth;				
	} else {
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return arrayPageSize;
}

function doLayerClose(){
	$("#lightbox-secNav-btnTopClose").click();
};

function doLayerOverlay(){
	var arrPageSizes = t___getPageSize(true);
	
	$('#jquery-overlay').css( {
		backgroundColor : '#000',
		opacity : 0.6,
		width : arrPageSizes[0],
		height : arrPageSizes[1]
	}).fadeIn();
};

function doLayerResize(_setWidth, _setHeight){
	
	var $element = document.getElementById("ibox_iframe");
	
	var boxWidth = 0;
	var boxHeight = 0;
	
	if(_setWidth == 0){
		boxWidth = $element.contentWindow.document.body.offsetWidth;	
	}else{
		boxWidth = _setWidth;
	}
	
	if(_setHeight == 0){
		$("#ibox_iframe").height($element.contentWindow.document.body.offsetHeight+63);		
		boxHeight = $element.contentWindow.document.body.offsetHeight+63;
	}else{
		$("#ibox_iframe").height(_setHeight+30);
		boxHeight = _setHeight+30;
	}
	
	var intWidth = (boxWidth + (10 * 2));
	var intHeight = (boxHeight + (10 * 2));
	
	if(_setWidth == 0){	
		$("#ibox_iframe").height(boxHeight);		
		$('#lightbox-container-image-box').animate( {
			height : intHeight+41
		}, 400);
		
	}else{
		$("#ibox_iframe").height(boxHeight);
		$("#ibox_iframe").width(boxWidth);
		
		$('#lightbox-container-image-box').animate( {
			width : intWidth-40,
			height : intHeight+41
		}, 400);
	}
	
	$('#lightbox-container-image-data-box').css( {
		width : boxWidth,
		height : boxHeight
	});
};

